package com.api.gbhg.huangwm.action;

import com.api.gbhg.daiyanlu.Dao.RecordSubjectsDao;
import com.api.gbhg.huangwm.dao.YuSuanTiaoZhengDao;
import com.customization.commons.LogTool;
import org.apache.commons.lang.StringUtils;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;
import weaver.general.BaseBean;
import weaver.general.TimeUtil;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.Property;
import weaver.soa.workflow.request.RequestInfo;

import java.util.*;

/**
 * @Author Huangwm
 * @Date 2022-08-15
 * @Version 1.0
 * @Description TODO 预算调整流程 追加时记录财政拨款核批渠道
 **/
public class NewRecordSubjects implements Action {

    private LogTool logTool = new LogTool("/log/gbhg/huangwm/action/NewRecordSubjects",false);
    private YuSuanTiaoZhengDao dao = new YuSuanTiaoZhengDao();

    private static String YJ ;//科目级别
    private static String XJ ;//科目下级

    private String sql = "insert into uf_gxmdygnkm(xmlcid,dygnkmmc,kmdm,dygnkmbid,zfqd,lb,modedatacreater,modedatacreatedate,modedatacreatertype,modedatacreatetime,formmodeid,modeuuid,kmjxj,hpssjid)values(?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    public String execute(RequestInfo requestInfo) {
        int requestid = requestInfo.getRequestManager().getRequestid();
        int billid = requestInfo.getRequestManager().getBillid();
        String tablename = requestInfo.getRequestManager().getBillTableName();
        int formid = requestInfo.getRequestManager().getFormid();

        String tableGxmdygnkm = "uf_gxmdygnkm";
        String uuid = UUID.randomUUID().toString();
        String modeId = RecordSubjectsDao.getModeId(tableGxmdygnkm);//获取建模表Id
        String currentDateString = TimeUtil.getCurrentDateString();//获取当前日期
        String currentTimeString = TimeUtil.getOnlyCurrentTimeString();//获取当前时间

        BaseBean baseBean = new BaseBean();
        YJ = baseBean.getPropValue("Verification","yj");
        XJ = baseBean.getPropValue("Verification","xj");
        logTool.writeLog("财政拨款核批层级 YJ="+YJ+"  XJ="+XJ);

        String flowtype = "";
        Property[] properties = requestInfo.getMainTableInfo().getProperty();
        for(Property property : properties){
            if(property.getName().equals("flowtype")){
                flowtype = property.getValue();
            }
        }
        if(flowtype.equals("") || flowtype.equals("null")){
            requestInfo.getRequestManager().setMessagecontent("同步经费额度版本数据异常 未获取到表单flowtype");
            requestInfo.getRequestManager().setMessageid(requestid+"");
            return FAILURE_AND_CONTINUE;
        }
        List<List<Object>> datas = new ArrayList<>();
        RecordSetTrans rst = new RecordSetTrans();
        rst.setAutoCommit(false);
        try {
            String JJFL = dao.getJjfl_DtTablesNoZSXM(formid);//获取细化经济分类明细表名称
            List<String> HPQD = dao.getHpqd_DtTablesNoZSXM(formid);//获取核批渠道明细表名称

            RecordSet rs = new RecordSet();
            String sql1 = "select dzlx,dzxm,lcbj,ysnd,sydw,sbdw,xmmc,jbr from " + tablename + " where id=" + billid;
            rs.execute(sql1);
            rs.next();
            int dzlx = rs.getInt("dzlx");

            if(dzlx==1){
                //获取上年结转和当年预算的明细数据
                RecordSet snjz = new RecordSet();
                snjz.execute("select * from "+HPQD.get(1) +" where mainid="+billid);
                RecordSet dnys = new RecordSet();
                dnys.execute("select * from "+HPQD.get(0) +" where mainid="+billid);

                 Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);//当前年份
                int year1 = calendar.get(Calendar.YEAR);//上年年份
                int count = 0;

                // 上年结转核批明细
                while (snjz.next()) {
                    int sum1 = 0;
                    int sum2 = 0;
                    //收入类型
                    String srlx = snjz.getString("srlx");
                    //中央财政拨款项目
                    String zyczbksr = snjz.getString("zyczbksr");
                    //支付渠道-上年结转
                    String zfqd = "1";
                    if (srlx.equals("0")) {//0：中央
                        if (StringUtils.isNotBlank(zyczbksr)) {
                            String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                            sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                            sum2 = Integer.parseInt(sta[1]);//表id
                            if (sum1 == Integer.parseInt(YJ)) {
                                RecordSet sub = subKM(sum2);
                                int gnkmid = sub.getInt("gnkmid");
                                String gnkmmc = sub.getString("kmmc");
                                String kmdm = sub.getString("kmdm");
                                List<Object> insertData = new ArrayList<>();
                                insertData.add(requestid);//xmlcid
                                insertData.add(gnkmmc);//dygnkmmc
                                insertData.add(kmdm);//kmdm
                                insertData.add(gnkmid);//dygnkmbid
                                insertData.add(zfqd);//zfqd
                                insertData.add("0");//lb
                                insertData.add("1");//modedatacreater
                                insertData.add(currentDateString);//modedatacreatedate
                                insertData.add("1");//modedatacreatertype
                                insertData.add(currentTimeString);//modedatacreatetime
                                insertData.add(modeId);//formmodeid
                                insertData.add(uuid);//modeuuid
                                insertData.add("0");//kmjxj
                                insertData.add(zyczbksr);//hpssjid

                                datas.add(insertData);
                            } else if (sum1 == Integer.parseInt(XJ)) {
                                RecordSet sub = subXJ(sum2);
                                int gnkmid = sub.getInt("gnkmid");
                                String gnkmmc = sub.getString("kmmc");
                                String kmdm = sub.getString("kmdm");
                                List<Object> insertData = new ArrayList<>();
                                insertData.add(requestid);//xmlcid
                                insertData.add(gnkmmc);//dygnkmmc
                                insertData.add(kmdm);//kmdm
                                insertData.add(gnkmid);//dygnkmbid
                                insertData.add(zfqd);//zfqd
                                insertData.add("0");//lb
                                insertData.add("1");//modedatacreater
                                insertData.add(currentDateString);//modedatacreatedate
                                insertData.add("1");//modedatacreatertype
                                insertData.add(currentTimeString);//modedatacreatetime
                                insertData.add(modeId);//formmodeid
                                insertData.add(uuid);//modeuuid
                                insertData.add("1");//kmjxj
                                insertData.add(zyczbksr);//hpssjid

                                datas.add(insertData);
                            }
                        }
                    }

                }

                // 当年预算核批明细
                while (dnys.next()) {
                    int sum1 = 0;
                    int sum2 = 0;
                    //收入类型
                    String srlx = dnys.getString("srlx");
                    //中央财政拨款收入
                    String zyczbksr = dnys.getString("zyczbksr");
                    //支付渠道-当年预算
                    String zfqd = "0";
                    if (srlx.equals("0")) {//0：中央
                        if (StringUtils.isNotBlank(zyczbksr)) {
                            String[] sta = zyczbksr.split("_");//当年结转核批下划线分割
                            sum1 = Integer.parseInt(sta[0]);//9为科目级，10为科目下级
                            sum2 = Integer.parseInt(sta[1]);//表id
                            if (sum1 == Integer.parseInt(YJ)) {
                                RecordSet sub = subKM(sum2);
                                int gnkmid = sub.getInt("gnkmid");
                                String gnkmmc = sub.getString("kmmc");
                                String kmdm = sub.getString("kmdm");
                                List<Object> insertData = new ArrayList<>();
                                insertData.add(requestid);//xmlcid
                                insertData.add(gnkmmc);//dygnkmmc
                                insertData.add(kmdm);//kmdm
                                insertData.add(gnkmid);//dygnkmbid
                                insertData.add(zfqd);//zfqd
                                insertData.add("0");//lb
                                insertData.add("1");//modedatacreater
                                insertData.add(currentDateString);//modedatacreatedate
                                insertData.add("1");//modedatacreatertype
                                insertData.add(currentTimeString);//modedatacreatetime
                                insertData.add(modeId);//formmodeid
                                insertData.add(uuid);//modeuuid
                                insertData.add("0");//kmjxj
                                insertData.add(zyczbksr);//hpssjid

                                datas.add(insertData);
                            } else if (sum1 == Integer.parseInt(XJ)) {
                                RecordSet sub = subXJ(sum2);
                                int gnkmid = sub.getInt("gnkmid");
                                String gnkmmc = sub.getString("kmmc");
                                String kmdm = sub.getString("kmdm");
                                List<Object> insertData = new ArrayList<>();
                                insertData.add(requestid);//xmlcid
                                insertData.add(gnkmmc);//dygnkmmc
                                insertData.add(kmdm);//kmdm
                                insertData.add(gnkmid);//dygnkmbid
                                insertData.add(zfqd);//zfqd
                                insertData.add("0");//lb
                                insertData.add("1");//modedatacreater
                                insertData.add(currentDateString);//modedatacreatedate
                                insertData.add("1");//modedatacreatertype
                                insertData.add(currentTimeString);//modedatacreatetime
                                insertData.add(modeId);//formmodeid
                                insertData.add(uuid);//modeuuid
                                insertData.add("1");//kmjxj
                                insertData.add(zyczbksr);//hpssjid

                                datas.add(insertData);
                            }
                        }
                    }
                }
            }
            //插入数据
            rst.executeBatchSql(sql,datas);
        }catch (Exception e){
            rst.rollback();
            e.printStackTrace();
            logTool.writeLog("预算调整追加时记录项目功能科目 msg="+e.getMessage());
            requestInfo.getRequestManager().setMessageid(requestInfo.getRequestid());
            requestInfo.getRequestManager().setMessagecontent("预算调整追加时记录项目功能科目 "+e.getMessage());
            return  FAILURE_AND_CONTINUE;
        }
        rst.commit();
        return SUCCESS;
    }

    /**
     * 科目级关联查询
     * @param id
     * @return
     */
    public RecordSet subKM(int id) {
        String sql = "select b.id as gnkmid,b.kmmc,a.gnkmmc as kmdm from uf_zyczbksr_km a left join uf_gnkm b" +
                " on a.gnkm=b.id where a.id="+id;
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs;
    }

    public RecordSet subXJ(int id) {
        String sql = "select b.gnkm as gnkmid,a.srzx as kmmc ,b.gnkmmc as kmdm  from uf_zyczbksr_xj a left join uf_zyczbksr_km b" +
                " on a.szgnkm=b.id where a.id="+id;
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs;
    }

    /**
     * 获取建模表id
     * @param tableName
     * @return
     */
    public static String getModeId(String tableName) {
        String sql = "select id from modeinfo where formid=(select id from workflow_bill where tablename='" + tableName + "')";
        RecordSet rs = new RecordSet();
        rs.executeQuery(sql);
        rs.next();
        return rs.getString("id");
    }
}
